#!/usr/bin/perl
######################################################################
# test-vectors generation script
# Jon Johnson and James Carroll
# October 2008
# ECEn 628 - Brigham Young University
#
# We recommend piping the output to a file (grin)
#
######################################################################
use warnings;
use strict;

my ($op, $s0, $s1, $exp0, $exp1, $m0, $m1);
my ($exp, $m);
my ($i, $j, $k, $l);

# header used during development
# printf "op\ts0 exp0\tm0\t\ts1 exp1\tm1\t\t\n";

#
# The loops are in order:
#
# 1. Inner-most loop: mantissa1 increments from 0 to 1.0 * 2^1
# 2. Next value0 increments in a similar fashion, by incrementing first the mantissa and then the exponent
# 3. We flip the sign bit
# 4. Outer-most loop: we flip the operator bit
#

my $exp_max = (2**4)-1;
my $m_max = (2**5)-1;

for (0..1) {  $op = $_;           # 0 == add; 1 == sub
for (0..1) {  $s0 = $_;           # sign
for (0..1) {  $s1 = $_;
for ($i =0; $i<$exp_max; $i+=7)   {  $exp0 = $i;     # mantissa
for ($j =0; $j<$exp_max; $j+=9)   {  $exp1 = $j; 
for ($k =0; $k<$m_max; $k+=71)   {  $m0 = $k;     # mantissa
for ($l =0; $l<$m_max; $l+=73)   {  $m1 = $l; 

# Example output
#   ( op => '0', arg0 => "0100100010", arg1 => "1000100010" result => "1000100010"),

# the Real Thing:

  # Skip on NaN
  if (($exp0 == $exp_max && $m0 != 0) || ($exp1 == $exp_max && $m1 != 0))  {
    next;
#    print "-- NaN!";
  }

  # Skip on denormals
  if (($exp0 == 0 && $m0 != 0) || ($exp1 == 0 && $m1 != 0))  {
    next;
#    print "-- denorm!";
  }

# Proof of concept:
#   printf "$op\t$s0 %0.4b\t%0.5b\t\t$s1 %0.4b\t%0.5b\n",$exp0,$m0,$exp1,$m1;

   printf "\t( op => \'$op\', arg0 => \"$s0%0.4b%0.5b\", arg1=> \"$s1%0.4b%0.5b\" result => \"\"),\n",$exp0,$m0,$exp1,$m1;

}}}}}}}

